﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Reflection;
using Keel.ORM;
using System.Data;
namespace Keel
{
    internal class KitVar
    {
       public static string AppConnectString =null  ;
       public static DbProviderFactory AppProviderFactory = null;
    }
    public class DBOperator<T>
    {

        IDatabase db = null;
        DbProviderFactory dbf = null;
        DbConnection cnt = null;
        public DBOperator()
        {
            db =Keel.DB.Common.NowDataBase ;
            dbf = db.GetProviderFactory();
            cnt = dbf.CreateConnection();
            cnt.ConnectionString = db.ConnectString;
        }
        public DBOperator(T t)
        {
            db = ((IDatabase)t );
            dbf = db.GetProviderFactory();
            cnt =  dbf.CreateConnection();
            cnt.ConnectionString = db.ConnectString;
        }
        public DbCommand CreateDbCommand()
        {
          return cnt.CreateCommand();
        }
        public int ExecuteNonQuery(DbCommand cmd,bool returnid)
        {
            int i = 0;

            try
            {
                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
                if (i == 1 && returnid)
                {
                    cmd.Parameters.Clear();
                    cmd.CommandText = "SELECT @@IDENTITY";
                    int.TryParse( cmd.ExecuteScalar().ToString (),out i );
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                cmd.Connection.Close();
            }
            return i;
        }

        public int ExecuteNonQuery(  DbCommand cmd)
        {
            int i = 0;

            try
            {
                cmd.Connection.Open();
                i = cmd.ExecuteNonQuery();
            }
            catch (Exception ex  )
            {
                throw ex;
            }
            finally
            {
                cmd.Connection.Close ();
            }
            return i;
        }


        /// <summary>
        /// 执行SQL语句，并返回影响行数
        /// </summary>
        /// <param name="sql">您要执行的SQL语句</param>
        /// <returns>影响行数</returns>
        public int ExecuteNonQuery(string sql)
        {
            int i = 0;
             
            try
            {
                cnt.Open();
                DbCommand dbc = cnt.CreateCommand();
                dbc.CommandText = sql;
                i = dbc.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                cnt.Close();
            }
            return i;
        }
        public DbDataReader   GetDataReader(string sql)
        {
            DbDataReader dr = null;
            cnt.Open();
            DbCommand dbc = cnt.CreateCommand();
            dbc.CommandText = sql;
            dr = dbc.ExecuteReader();
            return dr ;
        }
        public DbDataReader GetDataReader(DbCommand dbc)
        {
            DbDataReader dr = null;
            dbc.Connection.Open();
            dr = dbc.ExecuteReader();
            return dr;
        }
        public void Close()
        {
            if (cnt.State != ConnectionState.Closed)
            {
                cnt.Close();
            }
        }
        public DataTable  ExecuteFillDataTable(string sql)
        {
            DataTable  dt  = null;
            try
            {
                cnt.Open();
                DbCommand dbc = cnt.CreateCommand();
                dbc.CommandText = sql;
                DbDataAdapter da = dbf.CreateDataAdapter();
                da.SelectCommand = dbc;
                dt = new DataTable();
                da.Fill(dt );
            }
            finally
            {
                cnt.Close();
            }
            return dt;
        }
        public DataSet ExecuteFillDataSet(string sql)
        {
            DataSet  ds = null ;
            try
            {
                cnt.Open();
                DbCommand dbc = cnt.CreateCommand();
                dbc.CommandText = sql;
                DbDataAdapter da = dbf.CreateDataAdapter();
                da.SelectCommand  = dbc;
                ds = new DataSet();
                da.Fill(ds);
            }
            finally
            {
                cnt.Close();
            }
            return ds;
        }
    }
}
